﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageEmailLayout : Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_EMAILLAYOUT = "__EMAILLAYOUT::EMAILLAYOUT";
    private const string PROP_DATASRC_EMAILCATEGORY = "__EMAILCATEGORY::EMAILCATEGORY";
    private const string PROP_LAYOUT = "__EMAILLAYOUT::UIL";
    private const string PROP_SELECTED_EMAILLAYOUT_ID = "__DISPUTESUBCATEGORY::SDISPUTESUBCATEGORYID";

    #endregion

    #region Public Properties

    public EmailLayoutCollection DataSourceEmailLayout
    {
        get { return (SessionMgr.Session[PROP_DATASRC_EMAILLAYOUT] is EmailLayoutCollection ? (EmailLayoutCollection)SessionMgr.Session[PROP_DATASRC_EMAILLAYOUT] : (EmailLayoutCollection)(SessionMgr.Session[PROP_DATASRC_EMAILLAYOUT] = new EmailLayoutCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_EMAILLAYOUT] = value; }
    }

    public int SelectedEmailLayoutID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_EMAILLAYOUT_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_EMAILLAYOUT_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_EMAILLAYOUT_ID] = value; }
    }


    public EmailCategoryCollection DataSourceEmailCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_EMAILCATEGORY] is EmailCategoryCollection ? (EmailCategoryCollection)SessionMgr.Session[PROP_DATASRC_EMAILCATEGORY] : (EmailCategoryCollection)(SessionMgr.Session[PROP_DATASRC_EMAILCATEGORY] = new EmailCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_EMAILCATEGORY] = value; }
    }

    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }




    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceEmailLayout = EmailLayout.GetAllEmailLayouts();
            EmailLayoutList1.DataBind(DataSourceEmailLayout);
        }
    }

    #endregion

    #region Control: EmailLayoutList

    protected void ucEmailLayoutList_EditRequested(object sender, Controls_EmailLayoutList.EditRequestedEventArgs evt)
    {
        SelectedEmailLayoutID = evt.EL.EmailLayoutID;
        EmailLayoutViewEdit1.DataBind(evt.EL, DataSourceEmailCategory);
        SetLayout(LayoutType.Edit);
    }

    protected void ucEmailLayoutList_AddNewRequested(object sender, EventArgs evt)
    {
        EmailLayoutViewEdit1.CancelEdit();
        EmailLayout newEmailLayout = EmailLayout.CreateNew(DataSourceEmailLayout);
        SelectedEmailLayoutID = newEmailLayout.EmailLayoutID;
        DataSourceEmailCategory = EmailCategory.GetAllEmailCategories();
        EmailLayoutViewEdit1.DataBind(DataSourceEmailLayout.FindByEmailLayoutID(SelectedEmailLayoutID), DataSourceEmailCategory);
        SetLayout(LayoutType.Edit);
    }


    protected void ucEmailLayoutList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceEmailLayout = EmailLayout.GetAllEmailLayouts();
        EmailLayoutList1.DataSource = DataSourceEmailLayout;
    }

    #endregion

    #region Control: EmailLayoutViewEdit

    protected void ucEmailLayoutViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        EmailLayoutViewEdit1.DataSourceEmailLayout = DataSourceEmailLayout.FindByEmailLayoutID(SelectedEmailLayoutID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        EmailLayoutViewEdit1.CancelEdit();
        // If we were adding a new video...then remove the incomplete/not added new jobsubcategory object from the collection.
        if (DataSourceEmailLayout.FindByEmailLayoutID(SelectedEmailLayoutID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceEmailLayout.Remove(DataSourceEmailLayout.FindByEmailLayoutID(SelectedEmailLayoutID));
                SelectedEmailLayoutID = (DataSourceEmailLayout[0] != null ? DataSourceEmailLayout[0].EmailLayoutID: 0);
            }
            catch
            {
            }
        }
        EmailLayoutList1.DataBind(DataSourceEmailLayout);
        SetLayout(LayoutType.View);
    }

    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (EmailLayoutViewEdit1.EndEdit())
        {
            EmailLayout obj = DataSourceEmailLayout.FindByEmailLayoutID(SelectedEmailLayoutID);
            EmailLayout.Update(obj);

            // TODO: Set Feedback text...

            EmailLayoutList1.DataBind(DataSourceEmailLayout);
            SetLayout(LayoutType.View);

        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlEmailLayoutList.Visible = false;
                pnlEmailLayoutViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:

            case LayoutType.Search:

                pnlEmailLayoutList.Visible = true;
                pnlEmailLayoutViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion


}
